﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using seejee_Backendium.Core.Interfaces;
using seejee_Backendium.Core.Models;
using seejee_Backendium.Data.DbContest;

namespace seejee_Backendium.Data.Services
{
    public class UserAppService : IUserAPPsService
    {
        private readonly ApplicationDbContext _context;
        private readonly ILogger<MenuService> _logger;
        public UserAppService(ApplicationDbContext context, ILogger<MenuService> logger)
        {
            _context = context;
            _logger = logger;
        }
        public async Task<IEnumerable<UserApp>> GetAllAsync()
        {
            return await _context.userApps
                   .AsNoTracking()
                   .ToListAsync();
        }

        public async Task<UserApp> GetByIdAsync(String name)
        {
            return await _context.userApps
                  .AsNoTracking()
                  .FirstOrDefaultAsync(ua => ua.userName == name);
        }

        public async Task<IEnumerable<UserApp>> GetAllAsync(
        int page,
        int pageSize,
        string sortBy,
        string sortDirection)
        {
            var query = _context.userApps.AsNoTracking();

            // 动态排序 - 使用条件表达式
            query = sortBy.ToLower() switch
            {
                "id" => sortDirection.ToLower() == "desc"
                    ? query.OrderByDescending(ua => ua.id)
                    : query.OrderBy(ua => ua.id),
                "userName" => sortDirection.ToLower() == "desc"
                    ? query.OrderByDescending(ua => ua.userName)
                    : query.OrderBy(ua => ua.userName),
                "userInformation" => sortDirection.ToLower() == "desc"
                    ? query.OrderByDescending(ua => ua.userInformation)
                    : query.OrderBy(ua => ua.userInformation),
                "appName" => sortDirection.ToLower() == "desc"
                   ? query.OrderByDescending(ua => ua.appName)
                   : query.OrderBy(ua => ua.appName),
                "updateTime" => sortDirection.ToLower() == "desc"
                   ? query.OrderByDescending(ua => ua.updateTime)
                   : query.OrderBy(ua => ua.updateTime),
                _ => query.OrderBy(ua => ua.userName) // 默认排序
            };
            // 分页
            return await query
                .Skip((page - 1) * pageSize)
                .Take(pageSize)
                .ToListAsync();
        }
        public async Task<int> GetCountAsync()
        {
            return await _context.userApps.CountAsync();
        }
        //新增用户
        public async Task<UserApp> AddAsync(UserApp userApp)
        {
            // 检查是否已存在
            var existingUser = await _context.userApps
                .FirstOrDefaultAsync(ua => ua.userName == userApp.userName);
            if (existingUser != null)
            {
                throw new InvalidOperationException($"用户 {userApp.userName} 已存在");
            }
            _context.userApps.Add(userApp);
            await _context.SaveChangesAsync();

            return userApp;
        }
        public async Task<int> DelectByNameAsync(String name)
        {
            var Users = await _context.userApps
                        .Where(ua => ua.userName == name)
                        .ToListAsync();

            _context.userApps.RemoveRange(Users);
            return await _context.SaveChangesAsync();
        }
    }
}
